{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04c89b6",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#lambda-函数\" data-toc-modified-id=\"lambda-函数-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>lambda 函数</a></span></li><li><span><a href=\"#闭包\" data-toc-modified-id=\"闭包-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>闭包</a></span></li><li><span><a href=\"#内置高阶函数\" data-toc-modified-id=\"内置高阶函数-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>内置高阶函数</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a6a9bcc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6763d59",
   "metadata": {},
   "source": [
    "## lambda 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "27435683",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "function"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn = lambda x: x*x\n",
    "type(fn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eea8f582",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.<lambda>(x)>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dacd516b",
   "metadata": {},
   "source": [
    "## 闭包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d7973534",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "<function lazy_sum.<locals>.sum at 0x7f9cb1eff940>\n",
      "<function lazy_sum.<locals>.sum at 0x7f9cb1eff9d0>\n"
     ]
    }
   ],
   "source": [
    "def lazy_sum(*list):\n",
    "    def sum():\n",
    "        res=0\n",
    "        for i in list:\n",
    "            res+=i\n",
    "        return res\n",
    "    return sum\n",
    "\n",
    "f1 = lazy_sum(1,2,3,4)\n",
    "f2 = lazy_sum(1,2,3,4)\n",
    "\n",
    "print(f1 == f2)\n",
    "print(f1)\n",
    "print(f2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5da4b95",
   "metadata": {},
   "source": [
    "## 内置高阶函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6081920a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<map object at 0x7f9cb1f17df0>\n"
     ]
    }
   ],
   "source": [
    "squares = map(lambda x: x*x, range(0,10,2))\n",
    "print(squares)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6d589493",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 4, 16, 36, 64]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "square = list(squares)\n",
    "square"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8b8323c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[8, 11, 3]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wc = list(map(len, ['Windfury','WindWhisper','dog']))\n",
    "wc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "01ca2b7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 4, 5, 7, 8]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample = list(range(10))\n",
    "data = list(filter(lambda x: 0 if x%3==0 else 1, range(10)))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dcc66e03",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3.6",
   "language": "python",
   "name": "python3.6"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
